# encoding: utf-8
"""
@author     : Augus
@Data       : 2017/5/23
@Time       : 20:26
@File       : 爬取图片.py
@Software   : PyCharm 
@Version    : 
"""
import requests
import re
import os


"""
第一次爬虫过程，从头像吧爬取照片，保存在D盘的picture文件夹里
"""


def get_html(url):
    response = requests.get(url)
    response.encoding = 'utf-8'
    return response


def get_img(html):
    reg = r'<img class="BDE_Image" src="(.*?\.jpg)"'
    imgre = re.compile(reg)
    imglist = re.findall(imgre, html.text)
    return imglist


def main():
    base_url = 'http://tieba.baidu.com/p/5003263753'
    html = get_html(base_url)
    imglist = get_img(html)
    num = 1
    os.makedirs(r'D:\\picture\\第一次爬虫')
    for img in imglist:
        with open('D:\\picture\\第一次爬虫\\'+str(num)+'.jpg', 'wb') as file:
            file.write(get_html(img).content)
            num += 1


if __name__ == '__main__':
    main()
